package k.b.s;

import java.util.ArrayList;

import k.b.s.model.ChannelRowModel;
import k.b.s.model.ItemRowModel;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewActivity extends ListActivity {
	private ArrayList<ItemRowModel> items;
	private ArrayList<ChannelRowModel> rowModels;
	private RSSListAdapter mAdapter;
	private TextView tv;
	public Context ctx;
	private Spinner spinner;
	private int channelNo = 0;
	private int currentPosition;
	Update updateTask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		ctx = this;
		// OperationDB.updateItemTB(this);
		// update.execute();
		tv = (TextView) findViewById(R.id.text_channel);
		spinner = (Spinner) findViewById(R.id.spinner1);

	}

	@Override
	protected void onResume() {
		super.onResume();
//		update();
		updateTask = new Update();
		 updateTask.execute(null);
	}

	protected synchronized void update() {
		super.onResume();
		// リストデータの取得は�onResume
		rowModels = OperationDB.getChannelTB(this);
		if (!rowModels.isEmpty()) {
			int channelSize = rowModels.size();
			String[] sContent = new String[channelSize];
			for (int i = 0; i < channelSize; i++) {
				sContent[i] = rowModels.get(i).getTitle();
			}
			setSpinner(spinner, sContent);
			spinner.setSelection(currentPosition);
			spinner
					.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
						// アイテムが選択された時の動作
						public void onItemSelected(AdapterView parent,
								View view, final int position, final long id) {
							// Spinner を取得
							Spinner spinner = (Spinner) parent;
							// 選択されたアイテムのテキストを取得
							String str = spinner.getSelectedItem().toString();
							Log.v("STR", str + " " + position + " " + id);
							channelNo = position;
							getItems(channelNo);
							currentPosition = position;
						}

						// 何も選択されなかった時の動作
						public void onNothingSelected(AdapterView parent) {
						}
					});

			getItems(channelNo);
		}
	};

	private void getItems(int channelNo) {
		tv.setText(rowModels.get(channelNo).getTitle());
		String channel = rowModels.get(channelNo).getChannel();
		items = OperationDB.getItemFromChannelbyPubDate(this, channel, 15);
		if (items != null) {
			mAdapter = new RSSListAdapter(this, items);
			setListAdapter(mAdapter);
			// mAdapter.notifyDataSetChanged();
		}
		ctx = getApplication();

	}

	private void setSpinner(Spinner spinner, String[] arr) {
		ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_spinner_item, arr);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
	}

	public void onClickCancel(View v) {
		// fff = new LoadImageTask();
		// fff.cancel(true);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		ItemRowModel item = mAdapter.getItem(position);
		Intent intent = new Intent(this, DetailActivity.class);
		intent.putExtra("title", item.getTitle());
		intent.putExtra("detail", item.getDescription());
		intent.putExtra("channel", item.getChannel());
		intent.putExtra("channelNo", channelNo);
		startActivity(intent);
	}

	public void onClickRegist(View v) {
		Intent intent = new Intent(this, RssRegist.class);
		startActivity(intent);
	}

	public void onClickRenew(View v) {
		// OperationDB.updateItemTB(this);
		// onResume();

		Log.v("AsyncTask","updateTask.getStatus()" + updateTask.getStatus());

		if (updateTask.getStatus() != AsyncTask.Status.RUNNING) {
			updateTask = new Update();
			updateTask.execute(null);
		} else {
			Toast.makeText(this, "取得中です", Toast.LENGTH_SHORT).show();
		}
	}

	public static void makePopup(Context cont, String str) {
		Toast.makeText(cont, str, Toast.LENGTH_SHORT);
	}

	// public static void pullToReload(){
	// Intent intent = new Intent(ctx, ServiceStartActivity.class);
	// ctx.startService(intent);
	// }

	class Update extends AsyncTask<Void, Void, Void> {
		protected void onPreExecute() {
			// setProgressBarIndeterminateVisibility(true);
			Toast.makeText(getApplicationContext(), "取得", Toast.LENGTH_SHORT)
			.show();
		}

		// @Override
		// protected Void doInBackground() {
		// return null;
		// }
		protected void onPostExecute(Void result) {
			// setProgressBarIndeterminateVisibility(false);
			update();
			Toast.makeText(getApplicationContext(), "取得完了", Toast.LENGTH_SHORT)
					.show();
		}

		// protected void onProgressUpdate(Void...values){
		// adpter.notifyDataSetChanged();
		// }
		@Override
		protected Void doInBackground(Void... params) {
			// TODO 自動生成されたメソッド・スタブ
			Log.v("AsynctASK", "start doInbackGround");
			OperationDB.updateItemTB(ctx);
			Log.v("AsynctASK", "end doInbackGround");
			return null;
		}
	}
}